<?php

class CEmailHelper {
	
	//		$message = new Message ();
	//				$body = str_replace ( array (':username', ':comment' ), array ($model->school->school_name, $model->sponsor_comments ), Messages::model ()->getMessage ( 29 ) );
	//				$message->setBody ( $body, 'text/html' );
	//				
	//				$message->subject = 'A Comment added.';
	//
	//				foreach ( $contacts as $contact ) {
	//					$message->addTo ( $contact->email );
	//				}
	//				
	//				$message->addTo ( Yii::app ()->params ['adminEmail'] );
	//				
	//				$message->from = Yii::app ()->params ['adminEmail'];
	//				Yii::app ()->mail->send ( $message );
	//		

	public static function wellcome($mail,$username,$domain = null){
		$msg = array();
		
		$msg = array( 'to'=>$mail,
						'subject'=>'【名人吧】恭喜您，您的帐号开通啦！ ',
						'body'=>array('name'=>$username,'domain'=>$domain),
//						'from'=>'michael.song@mphasisortc.com',
						'view'=>'wellcome',
					
					);
	return 	CEmailHelper::send($msg);
		
	}
	/**
	 * @$msg Array
	 * @to @subject @body @view
	 */
	public static function send($msg = array()) {
		if (empty ( $msg )) {
			throw new CException ( "Message is empty;" );
		}
		if (! isset ( $msg ['to'] )) {
			throw new CException ( "There is no to address in Message." );
		}
		if (! isset ( $msg ['subject'] )) {
			throw new CException ( "There is no CException in Message." );
		}
		
		if (! isset ( $msg ['body'] )) {
			throw new CException ( "There is no body in Message." );
		}
	
//		if (! isset ( $msg ['view'] )) {
//			throw new CException ( "There is no view in Message." );
//		}
		
		$message = new Message ();
		
		$message->subject = $msg ['subject'];
		
		if(isset($msg['view'])){
			$message->view = $msg['view'];
		}
		
		$message->setBody ( $msg ['body'], 'text/html' );
		 
		if(is_array($msg['to'])){
			$message->setTo($msg['to']);
		}else{
			$message->addTo($msg['to']);
		}
		 if ( isset ( $msg ['from'] )) {
		 	$message->from = $msg ['from'];
		 }else
			$message->from = "support@mr8.me";
		
		return Yii::app()->mail->send($message);
	}
}

?>